$(document).ready(function(){
	getOrderDetail();
});

function getOrderDetail() {
	var requestUrl = "get_maintain_detail";
	var params = {
		order_id:selectedMaintainOrderId
	};
	
	$.post(requestUrl, params, function(data){
		var orderDetail = data.resultData;
		
		//记录当前订单的车牌
		selectedMaintainOrderLicense = orderDetail.licenseNumber;
		
		$("#mileage").val(orderDetail.mileage);
		$("#place_id").find("[value='" + orderDetail.placeId + "']").attr("selected", "selected");
		$("#comment").val(orderDetail.comment);
		$("#maintain_date").val(orderDetail.maintainDate);
		$("#total_fee").val(orderDetail.maintainFee);
		$("#status").val(getOrderStatusName(orderDetail.status));
		
		for (var i = 0; orderDetail.maintainItems && i < orderDetail.maintainItems.length; i++) {
			var item = orderDetail.maintainItems[i];
			
			maintainItems[maintainItems.length] = {
					item_id:item.itemId,
					item_brand:item.itemBrandName,
					item_type:item.itemTypeName,
					item_period:item.itemPeriodName,
					item_model:item.itemModel};
		}
		
		freshItemList();
		
		if (orderDetail.status == 0) {
			$("#div_return").css("display", "none");
		} else {
			$("#mileage").attr("disabled", "disabled");
			$("#place_id").attr("disabled", "disabled");
			$("#comment").attr("disabled", "disabled");
			$("#maintain_date").attr("disabled", "disabled");
			$("#total_fee").attr("disabled", "disabled");
			
			$(".maintain_item_control").css("display", "none");
		}
	}, "json");
}

function modifyMaintainOrder() {	
	if (checkMileage() && !errorMileage
			&& checkPlaceId() && !errorPlaceId
			&& checkTotalFee() && !errorTotalFee) {
		var requestUrl = "modify_maintain_order";
		var items = new Array();
		
		for(var i = 0; i < maintainItems.length; i++) {
			if (maintainItems[i] == null) {continue;}
			items[items.length] = maintainItems[i];
		}
		var params = {
			order_id:selectedMaintainOrderId,
			mileage:$("#mileage").val(),
			maintain_date:$("#maintain_date").val(),
			maintain_place:$("#place_id").val(),
			maintain_fee:$("#total_fee").val(),
			comment:$("#comment").val(),
			maintain_items:JSON.stringify(items)
		};
		
		$.post(requestUrl, params, function(data){
			showMaintainList();
		});
	}
}